package com.neutech.shop.controller;

import com.neutech.shop.entity.CartItem;
import com.neutech.shop.entity.Product;
import com.neutech.shop.entity.User;
import com.neutech.shop.service.CartItemService;
import com.neutech.shop.service.ProductService;
import com.neutech.shop.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author Neuedu-远哥
 * @version 1.0
 * @date 2021/9/22 16:17
 */
@Controller
@RequestMapping("/pre")
public class PreController {
    @Resource
    private UserService userService;
    @Resource
    private ProductService productService;
    @Resource
    private CartItemService cartItemService;



    @RequestMapping("/register")
    public String register(User user, String hcity, String hproper, String harea, String addr2){
        // harea=桥东区&hproper=石家庄市&hcity=河北省&addr2=A街道B小区
        // 注册用户的业务
        userService.register(user,hcity, hproper, harea, addr2);
        return "redirect:index.jsp";
    }
    @RequestMapping("/confirmUsername")
    public void confirmUsername(String username, HttpServletResponse response){
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            User user = userService.findByUsername(username);
            if(user == null){
                // 可以注册
                out.print("可以注册");
            }else{
                // 不能注册
                out.print("用户名已存在");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 用户登录
     * @param user
     * @param session
     * @return
     */
    @RequestMapping("/login")
    public String login(User user, HttpSession session,ModelMap map){
        boolean success = userService.login(user);
        if(success){
            // 登录成功的情况
            session.setAttribute("user",user);
            return "redirect:index.jsp";
        } else{
            // 登录失败
            map.addAttribute("error_msg","用户名或密码错误");
            // 重定向到登录页面 去登录
            return "login.jsp";
        }
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        // 让seesion马上失效
        session.invalidate();
        return "redirect:login.jsp";
    }
    @RequestMapping("/index")
    public String index(ModelMap map){
        // 最新商品
        List<Product> pList1 = productService.lastedProduct(0);
        List<Product> pList2 = productService.lastedProduct(4);
        List<Product> products = productService.findAll();
        List<CartItem> cartItems = cartItemService.selectAll();
        // 总计也绑定
        Double sum = cartItemService.getTotal();
        map.addAttribute("pList1",pList1);
        map.addAttribute("pList2",pList2);
        map.addAttribute("products",products);
        map.addAttribute("cartItems",cartItems);
        map.addAttribute("sum",sum);
        // 所有商品
        // 购物车内容
        return "forward:index.jsp";
    }
    @RequestMapping("/selectById")
    public String productDetail(Integer id, ModelMap map){

        Product product = productService.selectById(id);
        map.addAttribute("product",product);
        List<Product> pList1 = productService.lastedProduct(0);
        map.addAttribute("pList1",pList1);

        return "forward:detail.jsp";
    }
    @RequestMapping("/addToCart")
    public String addToCart(CartItem cartItem){
        cartItemService.insert(cartItem);
        return "redirect:cart.jsp";
    }
}
